<?php
    $this->headTitle('Xem tin','append');
    switch(strlen($this->proCheck['mobile'])) {        
    case 10:
        $this->proCheck['mobile'] = preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "$1 $2 $3", $this->proCheck['mobile']);
        break;
    case 11:
        $this->proCheck['mobile'] = preg_replace("/([0-9]{4})([0-9]{4})([0-9]{3})/", "$1 $2 $3", $this->proCheck['mobile']);
        break;
    default:        
        break;
    }
?>
<div class="col-md-9">
    <div class="mycontent maket-post">    
        <div class="navi">
            <?php echo $this->navi;?>                    
        </div>
        <h2>Quy Trình Đăng Tin</h2>        
        <button class="btn btn-success btn-circle btn-lg shape">1</button>
            <label>&nbsp; Tạo tin &raquo;&nbsp;</label>
        <button class="btn btn-warning btn-circle btn-lg shape">2</button>
            <label>&nbsp; Kiểm tra lại &raquo;&nbsp;</label>
        <button class="btn btn-success btn-circle btn-lg shape">3</button>
            <label>&nbsp; XONG!</label>
        <h2>Nội dung tin đăng</h2>
                
        <div class="block block-review">
            <div class="legend">
                <label>Thông tin cơ bản</label>
            </div>            
            <div class="col-md-3">
                <label>Bạn là:</label>
            </div>
            <div class="col-md-3">
                <p><?php echo $this->proCheck['human']?></p>                          
            </div>
            <div class="col-md-3">
                <label>Giao dịch: </label>
            </div>
            <div class="col-md-3">
                <p><?php echo $this->proCheck['transaction']?></p>      
            </div>
            <div class="col-md-3">
                <label>Khu vực:</label>
            </div>
            <div class="col-md-9">
                <p><?php echo $this->proCheck['areaName']?></p>
            </div>                
            <div class="col-md-3">
                <label>Danh mục:</label>                     
            </div>
            <div class="col-md-9">
                <p><?php echo $this->proCheck['catName']?> &nbsp;&gt;&gt;&nbsp; <?php echo $this->proCheck['itemName']?></p>
            </div>
            <div class="col-md-3">
                <?php echo (isset($this->proCheck['startDate']))?'<label>Ngày bắt đầu đăng tin:</label>':'';?>                     
            </div>
            <div class="col-md-9">
                <?php echo (isset($this->proCheck['startDate']))?'<p>'.date('d-m-Y',$this->proCheck['startDate']).'</p>':'';?>
            </div>
            <div class="col-md-3">
                <?php echo (isset($this->proCheck['endDate']))?'<label>Ngày kết thúc:</label>':'';?>                     
            </div>
            <div class="col-md-3">
                <?php echo (isset($this->proCheck['endDate']))?'<p>'.date('d-m-Y',$this->proCheck['endDate']).'</p>':'';?>                     
            </div>
            <div class="col-md-3">
                <?php echo (isset($this->proCheck['priceAd']))?'<label>Phí đăng tin:</label>':'';?>                     
            </div>
            <div class="col-md-3">
                <?php echo (isset($this->proCheck['priceAd']))?'<p>'.number_format($this->proCheck['priceAd'],0,'.',',').' VNĐ</p>':'';?>                     
            </div>
            <div class="clearfix"></div>
                           
        </div>
        <div class="block block-review">
            <div class="legend">
                <label>Nội dung tin đăng</label>
            </div>
            <h3><?php echo $this->proCheck['title']?></h3>
            <p><?php echo $this->proCheck['content']?></p>
            <div class="block image" style="text-align: center;">
                <div class="legend">
                    <label>Hình ảnh</label>
                </div>
            </div> 
            <div class="col-md-2">
                <label>Giá:</label>
            </div>
            <div class="col-md-10">
                <p><?php echo number_format($this->proCheck['price'],0,'.',',')?> VNĐ</p>
            </div>           
            <div class="clearfix"></div>                        
        </div>    
        <div class="block block-review">
            <div class="legend">
                <label>Thông tin liên hệ</label>
            </div>
            <div class="col-md-2">
                <label>Người liên hệ:</label>
            </div>
            <div class="col-md-10">
                <p><?php echo $this->proCheck['contact']?>&nbsp;</p>
            </div>
            <div class="col-md-2">
                <label>Số di động:</label>
            </div>
            <div class="col-md-10">
                <p><?php echo $this->proCheck['mobile']?>&nbsp;</p>
            </div>
            <div class="col-md-2">
                <label>Email liên hệ:</label>
            </div>
            <div class="col-md-10">
                <p><?php echo $this->proCheck['emailCont']?>&nbsp;</p>
            </div>
            <div class="col-md-2">                                                
                <label>Địa chỉ liên hệ:</label>
            </div>
            <div class="col-md-10">                                                
                <p><?php echo $this->proCheck['addressCont']?>&nbsp;</p>                
            </div>            
            <div class="clearfix"></div>
        </div>                
        <div class="col-md-12">
            <center>
                 <form class="form-inline" action="product/review" method="post" style="margin-bottom:20px;">
                    <input type="hidden" name="id" value="<?php echo $this->proCheck['id']?>"/>
                    <input type="hidden" name="type" value="<?php echo $this->proCheck['type']?>"/>                        
                    <button style="width:150px;background-color:ForestGreen;" name="direc" value="submit" type="submit" class="form-control">Đồng ý đăng</button>
                    <button style="width:150px;background-color:FireBrick;" name="direc" value="cancel" type="cancel" class="form-control">Huỷ đăng tin</button>
                    <button style="width:150px;background-color:DodgerBlue;" name="direc" value="edit" type="cancel" class="form-control">Sửa tin đăng</button>
                 </form>
            </center>
        </div>                          
    </div>
</div>
<?php    
    $image = json_decode($this->proCheck['photo']);
    //pr($image);               
?>
<script type="text/javascript">    
    <?php
        $i=0;                            
        foreach($image as $k => $v){
    ?>            
            $(".image").append(
                '<img id="photo-<?php echo $i?>" src="<?php echo $v?>" onerror="this.onerror=null;this.src=\'/public/static/images/noimage.jpg\';"/>'                
            );    
    <?php
            $i++;
        }        
    ?>    
</script>
<script type="text/javascript">  
    $('dt#photo-label').after(
        '<div id="photo-element" style="margin-bottom:10px">'+
    <?php 
        $i=1;
        foreach($image as $k => $v){        
    ?>                
            '<img src ="<?php echo $v?>" style="width:100px;height:100px;margin-right:5px" onerror="this.onerror=null;this.src=\'/public/static/images/noimage.jpg\';" >'+                
    <?php          
        }                       
    ?>                           
    '</div>'                       
    );     
</script>
<script type="text/javascript">           			
    function daydiff(first, second) {
    	return (second-first)/(1000*60*60*24)		
	}
    function numberWithCommas(x) {
        x = x.toString(); var pattern = /(-?\d+)(\d{3})/; while (pattern.test(x))
        x = x.replace(pattern, "$1,$2"); return x;
    }
    function readURL(input,id) {                
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                $('img#'+id).attr('src',e.target.result);                                
            }
            reader.readAsDataURL(input.files[0]);
        }
    }    
    function handleTypeNo() {
        $("select#userId").val(0);
        $('input[name=priceAd]').val('0 VNĐ');
    }
    function handleTypeVi() {
        var start = $("input[type=text][name=startDate]").val();                         
        var end = $("input[type=text][name=endDate]").val();
        var tempDate = start.split(" ");var start = tempDate[0].split("-");
        var tempDate = end.split(" ");var end = tempDate[0].split("-");                                    
        var start = new Date((start[0]) , (start[1]-1), (start[2]));
        var end = new Date((end[0]) , (end[1]-1), (end[2]));
        var numDate = daydiff(start, end);
        if(numDate < 0){                
            //alert("Ngày kết thúc bài đăng phải nhỏ hơn ngày bắt đầu !!");
            $("input[name=priceAd]").val('');                                
            return false;
        }else{
            var price =  numberWithCommas((numDate+1)*<?php echo $price['price'] ?>);                       
            $('input[name=priceAd]').val(price+' VNĐ');
        }
    }
    $(document).ready(function(){
        $('label[for="photo"]').html('Hình ảnh - Thay đổi hình ảnh (bạn phải upload lại toàn bộ album)');
        $("input[type=file]").each(function(){
            if($(this).attr('id') != 'photo-0' ){
                $(this).attr('style','display:none');
            }                        
        });                    
        var price = numberWithCommas($("input[name=priceAd]").val());                       
        $("input[name=priceAd]").val(price+' VNĐ');                                      
    });                          
    $(document).ready(function(){        
        $("input[type=file]").change(function(){
            //Reset uploaded picture
            $('#photo-element').html('');     
            //Review Picture
            var id = $(this).attr('id');
            var id = id.split('-');     var ime = "photo-"+id[1];        
            var id = parseInt(id[1])+1;                                    
            $("input#photo-"+id).attr('style','');                                                
            if( $('img#'+ime).length ){
                readURL(this,ime);
            }else{                
                $(this).before(
                    '<img id="'+ime+'" src="" style="width:100px;height:100px"  onerror="this.onerror=null;this.src=\'/public/static/images/noimage.jpg\';"/>'
                );
                readURL(this,ime);
            }                                                                                                                        
        });                        
        $("select#itemId").change(function(){            
            var transaction = $("select#transaction :selected").text();
            var itemName = $("select#itemId :selected").text();
            $('input[type=text][name=title]').val(transaction+' '+itemName);                        
        });                                    
        $("input[type=text][name=endDate]").change(function(){            
            if($("select#type :selected").val() == 'vi'){
                handleTypeVi();   
            }else{
                handleTypeNo();
            }                                 
        });
        $("input[type=text][name=startDate]").change(function(){
            if($("select#type :selected").val() == 'vi'){
                handleTypeVi();
            }else{
                handleTypeNo();
            }                       
        }); 
        $("select#type").change(function(){            
            var val = $("select#type :selected").val();
            if(val == 'no'){                
                handleTypeNo();                
            }else{
                handleTypeVi();
            }           
        });
        $("select#userId").change(function(){
            var val = $("select#type :selected").val();
            if(val == 'no'){
                handleTypeNo();
            }      
        });
        $('#submitbutton').click(function(){
            var create = $("input[type=text][name=createDate]").val();
            var start = $("input[type=text][name=startDate]").val();                         
            var end = $("input[type=text][name=endDate]").val();
            var tempDate = create.split(" ");var createD = tempDate[0].split("-");var createT = tempDate[1].split(":");
            var tempDate = start.split(" ");var startD = tempDate[0].split("-");var startT = tempDate[1].split(":");                 
            var tempDate = end.split(" ");var endD = tempDate[0].split("-");var endT = tempDate[1].split(":");                                            
            var createD = new Date(createD[0] , (createD[1]-1), createD[2], createT[0], createT[1]);
            var startD = new Date(startD[0] , (startD[1]-1), startD[2], startT[0], startT[1]);                
            var endD = new Date(endD[0] , (endD[1]-1), endD[2], endT[0], endT[1]);
            if(startD - createD < 0 || endD - startD < 0){
                alert('Thời điểm tạo tin < Thời điểm đăng tin < Thời điểm kết thúc');
                return false;
            }                                        
            if($("select#type :selected").val() == 'vi'){                
                //var numDate = daydiff(startD, endD);                                                                                
                if($('select#userId :selected').val() == '0'){
                    alert("Cần chọn tài khoản sở hữu cho loại tin VIP!!");
                    return false;
                }
            }                                                                    
        });            
    });    				
</script>


    
